package com.reverie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reverie.entity.Region;
import com.reverie.mapper.RegionMapper;
import com.reverie.service.IRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Rui.Zhou
 * @since 2024-02-03
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements IRegionService {

    @Override
    public List<Region> queryTree() {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Region::getEnabled,true);
        List<Region> regionList =  list();
        List<Region> provinceList = regionList.stream().filter((region)-> region.getParentId() == 1).collect(Collectors.toList());

        provinceList.forEach((pregion)->{
            List<Region> regions = regionList.stream().filter(region -> region.getParentId().equals(pregion.getRegionId())).collect(Collectors.toList());
            regions.forEach(cregion->{
                List<Region> dicRegions = regionList.stream().filter(region -> region.getParentId().equals(cregion.getRegionId())).collect(Collectors.toList());
                cregion.setChildren(dicRegions);
            });
            pregion.setChildren(regions);
        });
        return provinceList;
    }
}
